<template>
	<view class="box">
		<view>
			<drawer :show="isDrawerOpen" @close="closeDrawer"></drawer>
		</view>
		<view>
			<languageMask :show="isOpenMask" @close="closeMask" @languageChanged="refreshLanguage"></languageMask>
		</view>
		<view class="head">
			<view style="display: flex;align-items: center;">
				<image src="../../static/qukuailian2_slices/danlieliebiao.png" class="list" @click="openDrawer"></image>
				<image src="../../static/caifubeifen_slices/lujing9.png" mode="" class="back" @click="toDetails">
				</image>
			</view>
			<view class="head-title">
				<view class="vtitle">{{$data.$language.tranAcc}}</view>
			</view>
			<view class="right-head">
				<image src="../../static/qianbaobeifen2_slices/diqiugenghuan.png" mode="" class="language"
					@click="openMask">
				</image>
			</view>
		</view>
		<view class="b-mox-certer">
			<view class="rerc-text">
				<view class="rc-title">
					{{$data.$language.trlimit}}
				</view>
				<view class="rc-input">
					<view class="">
						USDT
					</view>
					<input type="number" :placeholder="$data.$language.Pleaseenter" v-model="munboxinput">
				</view>
				<view class="keyongs">
					{{$data.$language.Currently}}{{hdrawal.usdt}}USDT，<span
						@click="wholeBtn">{{$data.$language.Transferall}}</span>
				</view>
				<view class="keyongs">
					<!-- 提示：提现即销毁价值5%USDT的WONE币 -->
					{{$data.$language.montixiansunL}}
				</view>
			</view>
			<view class="btn" @click="nuberBtn">
				{{$data.$language.tranAcc}}
				<!-- {{$data.$language.AssetAllocation}} -->
			</view>
			<mescroll-uni class="mescroll-uni" ref="mescrollRef" :fixed="false" @init="mescrollInit"
				@down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
				<view class="mescroll-main">
					<block v-for="(item, index) in dataList" :key="index">
						<view class="mescroll-item">
							<view class="list-item-up">
								<view class="label">
									{{$data.$language.withdrawal}}
								</view>
								<view class="num">
									{{item.usdt}}USDT
								</view>
							</view>
							<view class="value">
								<view class="time">
									{{item.createtime | timestampToTime}}
								</view>
								<view class="type">
									<view v-if="item.status == 0" style="color: #E6A23C;">{{$data.$language.inReview}}</view>
									<view v-if="item.status == 2" style="color: #67C23A;">{{$data.$language.completed}}</view>
								</view>
							</view>
						</view>
					</block>
				</view>
			</mescroll-uni>
		</view>



		<!-- <view class="monbtncer" v-if="bntshuminbtn">

		</view>
		<view class="monboxndmrj" v-if="bntshuminbtn">
			<view class="viewtitle">
				{{$data.$language.Passwordinput}}
			</view>
			<input type="password" :placeholder="$data.$language.Passwordinput" v-model="password" :maxlength='6'>
			<view class="btnmaninds">
				<view class="aatione" @click="cancellationbtn()">
					{{$data.$language.cancellation}}
				</view>
				<view class="" @click="quBtns">
					{{$data.$language.qbtn}}
				</view>
			</view>
		</view> -->
	</view>
</template>

<script>
	import drawer from "../../my-components/drawer/drawer.vue"
	import languageMask from "../../my-components/languageMask/languageMask.vue"
	import zhCN from '@/locales/zh-CN.js'
	import enUS from '@/locales/en-US.js'
	import trCN from '@/locales/tr-CN.js'
	import {
		withdrawalGet,
		withdrawalPost,
		getWithList
	} from '@/api/index.js';

	import Web3 from 'web3';

	import MescrollMixin from 'mescroll-uni/mescroll-mixins.js';
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				// $language: uni.getStorageSync('locale') == 'en-US' ? enUS : zhCN,
				isDrawerOpen: false,
				isOpenMask: false,
				hdrawal: {},
				munboxinput: '',
				bntshuminbtn: false,
				password: '',

				mescroll: null,
				downOption: {},
				upOption: {
					page: {
						size: 10
					},
					noMoreSize: 5,
					empty: {
						tip: 'No Data'
					}
				},
				// 列表数据
				dataList: []

			}
		},
		onReachBottom() {},
		filters: {
			timestampToTime(timestamp) {
				const date = new Date(timestamp * 1000);
				return date.toLocaleString();
			}
		},
		onLoad() {
			var translate = uni.getStorageSync('locale')
			if (translate == 'en-US') {
				this.$data.$language = enUS
			} else if (translate == 'zh-CN') {
				this.$data.$language = zhCN
			} else {
				this.$data.$language = trCN
			}
			this.maxData()
		},
		methods: {
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			downCallback() {
				this.mescroll.resetUpScroll();
			},
			upCallback({
				num,
				size
			}) {
				getWithList({
						limit: size,
						page: num
					}, (res) => {
						if (res.code == 1) {
							const {
								total,
								data
							} = res.data;
							if (num === 1) this.dataList = [];
							this.dataList = this.dataList.concat(data);
							this.$nextTick(() => {
								this.mescroll.endBySize(this.dataList.length, total);
							});
						} else {
							this.mescroll.endErr();
							uni.showToast({
								title: res.msg,
								duration: 2000,
								icon: 'error',
								mask: true
							});
						}
					},
					(errorMsg) => {

					},
				);
			},
			cancellationbtn() {
				this.bntshuminbtn = false
			},
			// 全部提现
			wholeBtn() {
				this.munboxinput = this.hdrawal.usdt
			},
			quBtns(sign, time) {


				var awalsucce = this.$data.$language.awalsucce
				this.bntshuminbtn = false
				withdrawalPost({
						usdt: this.munboxinput,
						// password: this.password,
						sign,
						time
					},
					(data) => {
						// 处理成功返回的数据
						// console.log(data,'123456')
						uni.showToast({
							title: awalsucce,
							icon: "none",
							duration: 2000
						})
						this.maxData()
						this.munboxinput = ''

					},
					(errorMsg) => {
						// 处理请求失败或错误的情况
						var mimacuowu = this.$data.$language.mimacuowu
						uni.showToast({
							title: mimacuowu,
							icon: "none",
							duration: 2000
						})
						console.error(errorMsg);
					}
				);
			},
			//提现按钮
			async nuberBtn() {
				if (Number(this.munboxinput) < 0) {
					uni.showToast({
						title: this.$data.$language.min0,
						icon: 'none',
						duration: 2000
					})
					return
				}

				// 钱包地址
				var accountsUrl = uni.getStorageSync('accountsUrl')

				try {

					const timestamp = Math.floor(Date.now() / 1000);
					// console.log(timestamp);
					// const web3 = new Web3(window.ethereum);
					// const sha3 = web3.utils.sha3("\x19Ethereum Signed Message:\n"+'hello'.length + 'hello')
					// console.log('sha3', sha3);


					// const accountsUrlNo0x = accountsUrl.substring(2)
					// console.log(accountsUrlNo0x);
					// console.log(accountsUrlNo0x.substring(0, 5));

					const signature = await window.ethereum.request({
						method: 'personal_sign',
						params: ['hello' + timestamp, accountsUrl],
					});



					// this.signature = signature;
					console.log('accountsUrl', accountsUrl);
					console.log('Signature:', signature);
					this.quBtns(signature, timestamp)
				} catch (error) {
					console.error('Signing Error:', error);
				}
				return
				var awalsucce1 = this.$data.$language.awalsucce1 + this.hdrawal.usdt
				if (parseFloat(this.munboxinput) > parseFloat(this.hdrawal.usdt)) {
					uni.showToast({
						title: awalsucce1,
						icon: "none",
						duration: 2000
					})
				} else if (this.munboxinput == '') {
					uni.showToast({
						title: this.$data.$language.Pleaseenter,
						icon: "none",
						duration: 2000
					})
				} else {
					// this.bntshuminbtn = true
					// // this.quBtns()
					// 钱包地址
					var accountsUrl = uni.getStorageSync('accountsUrl')
					try {

						const signature = await window.ethereum.request({
							method: 'personal_sign',
							params: [accountsUrl, accountsUrl],
						});
						this.signature = signature;
						console.log('Signature:', signature);
					} catch (error) {
						console.error('Signing Error:', error);
					}
				}

			},
			//获取提现金额
			maxData() {
				withdrawalGet((data) => {
						// 处理成功返回的数据
						this.hdrawal = data
						console.log(data, 'this.logdata');

					},
					(errorMsg) => {
						// 处理请求失败或错误的情况
						console.error(errorMsg);
					}
				);
			},
			refreshLanguage(locale) {
				if (locale === 'zh-CN') {
					this.$language = zhCN
				} else if (locale === 'en-US') {
					this.$language = enUS
				} else {
					this.$language = trCN
				}
				uni.reLaunch({
					url: '/pages/tranAcc/tranAcc'
				});

			},
			toDetails() {
				uni.navigateTo({
					url: '/pages/walletAll/walletAll'
				})
				// uni.navigateBack({
				// 	delta:1
				// })
			},
			openDrawer() {
				this.isDrawerOpen = true
			},
			closeDrawer() {
				this.isDrawerOpen = false
			},
			openMask() {
				this.isOpenMask = true
			},
			closeMask() {
				this.isOpenMask = false; // 遮罩层guanbi事件处理
			},
		},

		components: {
			drawer,
			languageMask
		}
	}
</script>
<style lang="less">
	@import "./tranAcc.less";
</style>